package com.eascs.admin.o2o.loanAccount.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.loanAccount.entity.O2oCollectFee;
import com.eascs.admin.o2o.loanAccount.service.IO2oCollectFeesService;
import com.eascs.admin.o2o.loanAccount.serviceimpl.O2oCollectFeesService;
import com.eascs.admin.o2o.loanAccount.vo.O2oCollectFeesVo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.doc.excel.DefaultRowWriteHandler;
import com.eascs.common.doc.excel.ExcelApiHelper;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.crm.common.custnew.entity.CustomerEntity;
import com.eascs.crm.common.custnew.service.CustomerService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;

@Controller
@RequestMapping("/collectFees")
public class O2oCollectFeesController extends BaseMasterViewController {
	
	private final Logger logger = LoggerFactory.getLogger(O2oCollectFeesController.class);

	@Autowired
	IO2oCollectFeesService o2oCollectFeesService;
	@Autowired
	O2oCollectFeesService collectFeesService;

	@Reference
	CustomerService customerService;

	/**
	 * @Title: index
	 * @Description: 个人打款列表信息
	 * @author 张庆奇
	 * @param vo
	 * @return
	 * @return String
	 * @throws @date 2017年11月30日 下午5:04:32
	 */
	@RequestMapping("/index")
	public String index(O2oCollectFeesVo vo) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "CollectFees");
		vo.setPageSize(10);
		String o2ourl = "/collectFees/index";
		String loanName = vo.getLoanName() == null ? "" : vo.getLoanName();
		String documentNo = vo.getDocumentNo() == null ? "" : vo.getDocumentNo();
		String status = vo.getStatus() == null ? "" : vo.getStatus();
		String beginCreateDate = vo.getBeginCreateDate() == null ? "" : vo.getBeginCreateDate();
		String endCreateDate = vo.getEndCreateDate() == null ? "" : vo.getEndCreateDate();
		String createName = vo.getCreateName() == null ? "" : vo.getCreateName();
		String ltdName = vo.getLtdName() == null ? "" : vo.getLtdName();
		boolean queryAll = vo.isQueryAll();
		logger.info("queryAll={}", queryAll);
		o2ourl = UrlUtil.appendUrl(o2ourl, "loanName", loanName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "documentNo", documentNo);
		o2ourl = UrlUtil.appendUrl(o2ourl, "status", status);
		o2ourl = UrlUtil.appendUrl(o2ourl, "beginCreateDate", beginCreateDate);
		o2ourl = UrlUtil.appendUrl(o2ourl, "endCreateDate", endCreateDate);
		o2ourl = UrlUtil.appendUrl(o2ourl, "createName", createName);
		o2ourl = UrlUtil.appendUrl(o2ourl, "ltdName", ltdName);
		if (queryAll) {
			o2ourl = UrlUtil.appendUrl(o2ourl, "queryAll", queryAll + "");
		}
		o2ourl = o2ourl + "&pageIndex=";
		
		String export = "/collectFees/export";
		export = UrlUtil.appendUrl(export, "loanName", loanName);
		export = UrlUtil.appendUrl(export, "documentNo", documentNo);
		export = UrlUtil.appendUrl(export, "status", status);
		export = UrlUtil.appendUrl(export, "beginCreateDate", beginCreateDate);
		export = UrlUtil.appendUrl(export, "endCreateDate", endCreateDate);
		export = UrlUtil.appendUrl(export, "createName", createName);
		export = UrlUtil.appendUrl(export, "ltdName", ltdName);
		if (queryAll) {
			export = UrlUtil.appendUrl(export, "queryAll", queryAll + "");
		}
		export = export + "&pageIndex=";

		PageResult<Object[]> pageResult = o2oCollectFeesService.queryByPageCollectFee(vo);
		String titles[] = { "单号", "创建人", "经办平台", "姓名", "证件号码", "打款金额（元）", "创建时间", "单据状态" };
		SelectDataOption[] selectDataOptions = { null, null, null, null, null, null, null, new SelectDataOption("O2O_BILLSTATUS") };
		Button[] titleButtons = new Button[] { ButtonHelper.createOpenButton(null, "/collectFees/detail?type=VIEW&id=") };// 1)以弹窗的形式打开(ajax按钮)
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setFirstRenderColumnIndex(1);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setTitleButtons(titleButtons);
		ViewContext.put("paginationInfo", o2oPagingInfo);
		ViewContext.put("vo", vo);
		ViewContext.put("export", export);
		return "/collectFees/index";
	}

	/**
	 * @Title: create
	 * @Description:新增打款
	 * @author 张庆奇
	 * @param vo
	 * @return
	 * @return String
	 * @throws @date 2017年11月30日 下午5:04:07
	 */
	@RequestMapping("/search")
	public String search(O2oCollectFeesVo vo) {

		return "/collectFees/search";
	}
	
	@RequestMapping("/export")
	public void export(O2oCollectFeesVo vo,HttpServletResponse httpServletResponse){
		try {
			vo.setPageSize(Integer.MAX_VALUE);
			httpServletResponse.setHeader("Content-Disposition", "attachment;filename=collectfees.xls");
			httpServletResponse.setContentType("appliction/octet-stream;charset=utf-8");
			String sourceFileName = "/excelTemplate/collectfees.xls";
			PageResult<Object[]> pageResult = collectFeesService.queryByPageCollectFeeExport(vo);
			DefaultRowWriteHandler defaultRowWriteHandler = new DefaultRowWriteHandler(pageResult.getData());
			ExcelApiHelper.writeHandle(httpServletResponse.getOutputStream(), O2oCollectFeesController.class.getResourceAsStream(sourceFileName), defaultRowWriteHandler);
		} catch (IOException e) {
			logger.error("导出个人打款列表错误，信息：【{}】", e.getMessage());
			e.printStackTrace();
		}
	}

	/**
	 * @Title: addInfo
	 * @Description: 创建打款信息
	 * @author 张庆奇
	 * @param vo
	 * @return
	 * @return String
	 * @throws @date 2017年12月4日 下午1:18:49
	 */
	@RequestMapping(value = "/addInfo", produces = "text/html;charset=UTF-8")
	public String addInfo(O2oCollectFeesVo vo) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "ADDCollectFees");
		if (StringUtil.isNotBlank(vo.getDocumentNo())) {

			CustomerEntity basic = customerService.queryCustomerEntityByIdCard(vo.getDocumentNo());
			ViewContext.put("basic", basic);
		}
		ViewContext.put("vo", vo);

		return "collectFees/add_info";
	}

	/**
	 * @Description: <p>
	 *               打款管理, 打款信息
	 *               </p>
	 * @author yy
	 * @date 2017年11月30日
	 * @version V1.0
	 */
	@RequestMapping("/detail")
	public String detailInfo(String id, String type) {

		O2oCollectFee o2oCollectFee = o2oCollectFeesService.queryById(id);
		ViewContext.put("entity", o2oCollectFee);

		String url = "collectFees/edit_detail";
		if ("VIEW".equals(type)) {
			url = "collectFees/detail_view";

			String approvUrl = "/wf/processNode/agreeInfo?referId=" + id + "&amp;processType=ACCOUNT_REMITTANCE";
			ViewContext.put("approvUrl", approvUrl);
		}

		return url;
	}

}
